// ====================================================================================================================
// ======= 字典数据处理工具类
// ====================================================================================================================

import {getDicts} from "../api/system/dict/data";

/**
 * 将dicts字典数据，转换成适用uni组件的数据格式
 *
 * <p>
 * <br>格式如下：
 * <br> range: [
 * <br>     { value: 0, text: "标签", type: "success" },
 * <br>     { value: 1, text: "标签", type: "warning" },
 * <br>     { value: 2, text: "标签", type: "error"   },
 * <br> ],
 *
 * @param dicts 字典名称
 * @returns {number} 适用于Uni的数据格式
 */
export function getDictsByNameForUni(dictName) {
    let range = []
    getDicts(dictName).then(response => {
        let dicts = response.data;
        if (!!dicts && dicts.length > 0) {
            dicts.forEach((dict, index) => {
                let listClass;
                if (dict.listClass == "success") {
                    listClass = "success"
                } else if (dict.listClass == "warning") {
                    listClass = "warning"
                } else if (dict.listClass == "danger") {
                    listClass = "error"
                } else if (dict.listClass == "info") {
                    listClass = ""
                } else {
                    listClass = "primary"
                }
                range.push({value: parseInt(dict.dictValue), text: dict.dictLabel, type: listClass})
            });
        }
    });
    return range;
}

/**
 * 将dicts字典数据，转换成适用uni组件的数据格式
 *
 * <p>
 * <br>格式如下：
 * <br> range: [
 * <br>     { value: 0, text: "标签", type: "success" },
 * <br>     { value: 1, text: "标签", type: "warning" },
 * <br>     { value: 2, text: "标签", type: "error"   },
 * <br> ],
 *
 * @param dicts 字典数据
 * @returns {number} 适用于Uni的数据格式
 */
export function getDictsForUni(dicts) {
    let range = []
    if (!!dicts && dicts.length > 0) {
        dicts.forEach((dict, index) => {
            let listClass;
            if (dict.listClass == "success") {
                listClass = "success"
            } else if (dict.listClass == "warning") {
                listClass = "warning"
            } else if (dict.listClass == "danger") {
                listClass = "error"
            } else if (dict.listClass == "info") {
                listClass = ""
            } else {
                listClass = "primary"
            }
            range.push({value: parseInt(dict.dictValue), text: dict.dictLabel, type: listClass})
        });
    }
    return range;
}